package top.v5it.logback.push.util;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import top.v5it.logback.push.channel.LogInfo;

import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhanpu
 * @date 2021/7/21
 */
@Slf4j
public class StringTemplateUtil {

    private static final String MARKDOWN_CONTENT_TEMPLATE = "#### {}\n##### 级别`{}`\n> *{}* \n**类名:**\n*`{}`*\n**原因:**\n*{}*";
    private static final String TEXT_CONTENT_TEMPLATE = "{}\n级别「{}」\n类名:\n{}\n原因:\n{}";
    private static final String MAIL_CONTENT_TEMPLATE = "\n\n级别「{}」 \n\n类名:\n\n{}\n\n原因:\n\n {}";

    private StringTemplateUtil() {
        throw new UnsupportedOperationException();
    }

    /**
     * 用具体参数替换内容模板中变量
     *
     * @param contentTemp 内容模板
     * @param jsonParams  json字符形式参数
     * @return 完整内容
     */
    public static String replace(String contentTemp, String jsonParams) {
        Assert.notBlank(contentTemp);
        Assert.notBlank(jsonParams);
        final Map<String, Object> map = JSONUtil.parseObj(jsonParams);
        final Iterator<Map.Entry<String, Object>> iterator = map.entrySet().iterator();
        String content = contentTemp;
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            content = content.replace("{" + next.getKey() + "}", Objects.toString(next.getValue()));
        }
        // 替换后内容与原来内容一致说明是短信消息，将短信参数序列化返回
        return contentTemp.equals(content) ? contentTemp : content;
    }

    /**
     * 拼装markdown消息
     *
     * @param title    标题
     * @param severity 级别
     * @param content  内容
     * @return markdown内容
     */
    public static String markdown(String title, String severity, String timestamp, String logger, String content) {
        severity = Optional.of(severity)
                .filter(t -> StrUtil.equals("ERROR", t))
                .map(t -> "<font color='#dd0000'>" + t + "</font>")
                .orElse(severity);
        return StrUtil.format(MARKDOWN_CONTENT_TEMPLATE, title, severity, timestamp, StrUtil.subAfter(logger, '.', true), content);
    }

    /**
     * 拼装markdown消息
     *
     * @param logInfo 日志信息
     * @return markdown内容
     */
    public static String markdown(LogInfo logInfo) {
        return markdown(logInfo.getMessage().getTitle(),
                logInfo.getLevel(),
                logInfo.getTimestamp(),
                StrUtil.subAfter(logInfo.getLogger(), '.', true),
                logInfo.getMessage().getDescription());
    }

    /**
     * 拼装mail消息
     *
     * @param severity 级别
     * @param content  内容
     * @return mail内容
     */
    public static String mail(String severity, String logger, String content) {
        return StrUtil.format(MAIL_CONTENT_TEMPLATE, severity, logger, content);
    }

    /**
     * 拼装text消息
     *
     * @param title    标题
     * @param severity 级别
     * @param content  内容
     * @return text内容
     */
    public static String text(String title, String severity, String logger, String content) {
        return StrUtil.format(TEXT_CONTENT_TEMPLATE, title, severity, logger, content);
    }
}
